package org.mybatis.generator.codegen.mybatis3.xmlmapper.elements;

import org.mybatis.generator.api.IntrospectedColumn;
import org.mybatis.generator.api.dom.xml.Attribute;
import org.mybatis.generator.api.dom.xml.TextElement;
import org.mybatis.generator.api.dom.xml.XmlElement;
import org.mybatis.generator.codegen.mybatis3.ExtMyBatis3FormattingUtilities;
import org.mybatis.generator.codegen.mybatis3.MyBatis3FormattingUtilities;

import java.util.Iterator;

/*********************************************************
 * 文件名称：InsertColumnElementGenerator.java
 * 系统名称：cxycloud
 * 模块名称：org.mybatis.generator.codegen.mybatis3.xmlmapper.elements
 * 功能说明：
 * 开发人员 @author：caoxy
 * 开发时间 @date：2023/8/24 15:48
 * 修改记录：程序版本  修改日期  修改人员  修改单号  修改说明
 *********************************************************/
public class InsertColumnElementGenerator extends AbstractXmlElementGenerator {
    public InsertColumnElementGenerator() {
    }

    public void addElements(XmlElement parentElement) {
        XmlElement answer = new XmlElement("sql");
        answer.addAttribute(new Attribute("id", ExtMyBatis3FormattingUtilities.ATTR_INSERT_COLUMN_LIST_ID));
        this.context.getCommentGenerator().addComment(answer);
        StringBuilder sb = new StringBuilder();
        Iterator<IntrospectedColumn> iter = this.introspectedTable.getAllColumns().iterator();

        while(iter.hasNext()) {
            IntrospectedColumn introspectedColumn = (IntrospectedColumn)iter.next();
            if (ExtMyBatis3FormattingUtilities.insertSysdate(introspectedColumn)) {
                sb.append("${getDBDate}");
            } else {
                sb.append(MyBatis3FormattingUtilities.getParameterClause(introspectedColumn, "row."));
            }

            if (iter.hasNext()) {
                sb.append(", ");
            }

            if (sb.length() > 80) {
                answer.addElement(new TextElement(sb.toString()));
                sb.setLength(0);
            }
        }

        if (sb.length() > 0) {
            answer.addElement(new TextElement(sb.toString()));
        }

        if (this.context.getPlugins().sqlMapBlobColumnListElementGenerated(answer, this.introspectedTable)) {
            parentElement.addElement(answer);
        }

    }
}
